<template>
  <div class="app-container">
    <el-tabs
      v-model="activeTabName"
      type="card"
      class="demo-tabs"
      @tab-click="handleClick"
    >
      <el-tab-pane label="待处理的任务" name="1"></el-tab-pane>
      <el-tab-pane label="已处理的任务" name="2"></el-tab-pane>
    </el-tabs>
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="排查时间:" label-width="140px" prop="startDate">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          type="daterange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column
        label="排查任务名称"
        prop="listName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="排查周期"
        prop="checkCycleType"
        width="150"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseCheckCycle(scope.row.checkCycleType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查人员" prop="checkUserName" />

      <el-table-column label="排查开始时间" prop="startDate" width="260">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查结束时间" prop="endDate" width="260">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查时间" prop="checkTime" width="260">
        <template #default="scope">
          <span>{{ scope.row.checkTime ? parseTime(scope.row.checkTime) : "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查结果" prop="checkStatus">
        <template #default="scope">
          <span>{{ parseCheckState(scope.row.checkStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            v-if="scope.row.checkStatus == '3' || scope.row.checkStatus == '2'"
            content="去处理任务"
            placement="top"
          >
            <el-button link type="primary" plain @click="handleCheck(scope.row)"
              >去处理</el-button
            >
          </el-tooltip>
          <el-tooltip v-else content="详情" placement="top">
            <el-button
              link
              icon="CircleCheck"
              type="primary"
              @click="handleInfo(scope.row)"
              >详情</el-button
            >
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>
<script setup name="MyCheckTask">
import { listMyCheckTask } from "@/api/prevent/workbench/myCheck";
import { listPeriod } from "@/api/prevent/hidden/period";
import { listCheckTask, getCheckStateList } from "@/api/prevent/hidden/hiddenCheckTask";
const activeTabName = ref("1"); //默认是待处理的任务
const periodOption = ref([]); //隐患排查周期
const checkStateOption = ref([]); //排查状态
const { proxy } = getCurrentInstance();
const router = useRouter();
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const dateRange = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
    checkStatus: "3",
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

/** 处理排查任务的切换切换 */
const handleClick = (tab, event) => {
  if (tab.props.name == "1") {
    queryParams.value.checkStatus = "3";
  } else {
    queryParams.value.checkStatus = "1";
  }
  getList();
};

/** 获取隐患排查周期列表数据 */
async function getPeriodList() {
  listPeriod().then((response) => {
    periodOption.value = response.data;
  });
}

/** 获取排查结果状态列表数据 */
async function getCheckStateOPtion() {
  getCheckStateList().then((response) => {
    checkStateOption.value = response.data;
  });
}

/** 处理排查结果数据呈现 */
function parseCheckState(val) {
  var itemTarget = checkStateOption.value.filter((item) => {
    return item.code == val;
  });

  return itemTarget[0].value;
}

/** 处理排查周期数据 */
function parseCheckCycle(checkCycleType) {
  if (periodOption.value.length < 1) return "---";
  var itemTarget = periodOption.value.filter((item) => {
    return item.checkPeriodId == checkCycleType;
  });

  return itemTarget[0].checkPeriodName;
}

/** 处理搜索 */
function handleQuery() {}
/** 重置 */
function resetQuery() {}

/** 处理删除 */
function handleInfo(row) {
  router.push("/workbench/check/info/" + row.taskId);
}
/**获取数据列表 */
function getList() {
  loading.value = true;
  listMyCheckTask(queryParams.value).then((response) => {
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
    loading.value = false;
  });
}

/** 去处理我的排查任务 */
function handleCheck(row) {
  router.push("/workbench/check/handle/" + row.taskId);
}

getCheckStateOPtion();
getPeriodList();
getList();
</script>
